﻿
using Application.Users.Interfaces;
using Infrastructure.Data.Contexts;
using Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Shared.Configuration;

namespace Infrastructure
{
    public static class InfrastructureCollectionExtensions
    {
        /// <summary>
        /// 注册数据库服务
        /// </summary>
        /// <param name="services"></param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static IServiceCollection AddDatabaseContext(this IServiceCollection services, DatabaseSettings settings)
        {
            services.AddDbContext<AppDbContext>(options =>
                options.UseSqlServer(settings.ConnectionString,
                    sqlServerOptions => sqlServerOptions.EnableRetryOnFailure(
                        maxRetryCount: 5,
                        maxRetryDelay: TimeSpan.FromSeconds(settings.CommandTimeoutSeconds),
                        errorNumbersToAdd: null
                    )));

            return services;
        }

        /// <summary>
        /// 添加仓储服务
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddRepository(this IServiceCollection services)
        {
            services.AddScoped<IUserReadRepository, UserReadRepository>();
            services.AddScoped<IUserWriteRepository, UserWriteRepository>();
            return services;
        }

        
    }
}
